import Vue from 'vue'
import store from './store'
import * as filters from './filters'

function getLongShort(){
  const {long = {size: 0}, short = {size: 0}} = store.getters.account
  let totalLongPositionSize = parseInt(long.size || 0)
  let totalShortPositionSize = parseInt(short.size || 0)
  return {totalLongPositionSize, totalShortPositionSize}
}

Vue.mixin({
  computed: {
    getCurPaireObj: function() {
      let curPaireObj = store.getters.curPaireObj
      log('mixin.getCurPaireObj', {curPaireObj})
      return curPaireObj
    },
    totalHoldings: function() {//总持仓额
      let { totalLongPositionSize, totalShortPositionSize } = getLongShort()
      let {user} = store.getters
      let rs = (totalLongPositionSize + totalShortPositionSize) * user.spotPrice
      log('mixin.totalHoldings', {totalLongPositionSize, totalShortPositionSize, user, rs})
      return rs
    },
    oli: function(){//裸头寸额
      let { totalLongPositionSize, totalShortPositionSize } = getLongShort()
      let {user} = store.getters
      let rs = (totalLongPositionSize - totalShortPositionSize) * user.spotPrice
      log('mixin.oli', {totalLongPositionSize, totalShortPositionSize, user, rs})
      return rs
    },
    marginRemainder: function(){//保证金余额
      let {user} = store.getters
      log('mixin.marginRemainder', {user})
      return parseInt(user.account.accountBalance) + user.unrealizedPnl
    },
    maintainMargin: function(){//维持保证金
      let { totalLongPositionSize, totalShortPositionSize } = getLongShort()
      let {user} = store.getters
      let rs = (totalLongPositionSize + totalShortPositionSize) * user.spotPrice * parseInt(user.board.maintenanceMarginRatio) * 1e-6
      log('mixin.maintainMargin', {totalLongPositionSize, totalShortPositionSize, user, rs})
      return rs
    },
    marginRatio: function(){//保证金率
      let { totalLongPositionSize, totalShortPositionSize } = getLongShort()
      let {user} = store.getters
      let sum = totalLongPositionSize + totalShortPositionSize
      let rs = '--'
      if(sum === 0 || user.spotPrice === 0) {
        //
      } else {
        rs = (parseInt(user.account.accountBalance) + user.unrealizedPnl) / ((totalLongPositionSize + totalShortPositionSize) * user.spotPrice)
      }
      log('mixin.marginRatio', {totalLongPositionSize, totalShortPositionSize, user, rs})
      return rs
    }
  }
})
